export default {
  namespaced: true,
  state() {
    return {
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    };
  },
  mutations: {
    dell(state, index) {
      state.goodList.splice(index, 1);
    },
     subtractFn (state, payload) {
      if (state.goodList[payload.index].num || payload.num === 1) { state.goodList[payload.index].num += payload.num }
    },
    isAll(state, payload){
      state.goodList[payload].checked=!state.goodList[payload].checked
    }
  },
  getters: {
    sum(state) {
      return state.goodList
        .filter((item) => item.checked)
        .reduce((p, item) => p + item.price * item.num, 0);
    },
  },
};
